//
//  ViewController.m
//  addressBook
//
//  Created by apple on 15/8/20.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "Users.h"

@interface ViewController ()
{
    NSString *addressBookPath;
    NSMutableArray *array;
    Users *p;
    NSInteger currentIndex;
    NSInteger totalIndex;
}
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *sexTextField;
@property (weak, nonatomic) IBOutlet UITextField *companyTextField;
@property (weak, nonatomic) IBOutlet UITextField *positionTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *letterBoxTextField;
@property (weak, nonatomic) IBOutlet UITextField *weiXinTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建一个存储地址；
    addressBookPath = [NSHomeDirectory()
        stringByAppendingPathComponent:@"Documents/addressBook.plist"];
    NSLog(@"Path-->%@",addressBookPath);
    NSFileManager *fileManager = [NSFileManager defaultManager];
   
    array = [NSMutableArray array];
    
   //    [array writeToFile:addressBookPath atomically:YES];
    
    if (![fileManager fileExistsAtPath:addressBookPath])
    {
        [NSKeyedArchiver archiveRootObject:array toFile:addressBookPath];
    };
    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 点击按钮
- (IBAction)buttonClick:(UIButton *)sender {

    array = [NSKeyedUnarchiver unarchiveObjectWithFile:addressBookPath];
    BOOL beThere = NO;
    
    for ( p in array)
    {
        if ([p.name isEqualToString:self.nameTextField.text])
        {
            beThere = YES;
            break;
        }
    }
    
    
    //添加
    if ([sender.currentTitle isEqualToString:@"添加"])
    {
        if (beThere) {
            
        }
        else if (_nameTextField.text.length > 0 && _nameTextField.text.length <= 10)
        {
        Users *user = [[Users alloc] initWithName:_nameTextField.text sex:_sexTextField.text company:_companyTextField.text phoneNumber:_phoneNumberTextField.text letterBox:_letterBoxTextField.text weiXin:_weiXinTextField.text position:_positionTextField.text];
        
        [array addObject:user];
            
        [NSKeyedArchiver archiveRootObject:array toFile:addressBookPath];
        NSMutableArray *str = [NSKeyedUnarchiver unarchiveObjectWithFile:addressBookPath];
        NSLog(@"str归档--> %@",str);
            
        [self clearTextField];
        }
    }
    
    //删除；
    if ([sender.currentTitle isEqualToString:@"删除"])
    {
        if (beThere)
        {
            [array removeObject:p];
        }
        [NSKeyedArchiver archiveRootObject:array toFile:addressBookPath];
        NSMutableArray *str1 = [NSKeyedUnarchiver unarchiveObjectWithFile:addressBookPath];
        NSLog(@"str1--> %@",str1);
    }
    
    // 上一个；
    if ([sender.currentTitle isEqualToString:@"上一个"])
    {
        if ([array count] > 0)
        {
            currentIndex--;
            if (currentIndex < 0)
            {
                currentIndex = [array count] - 1;
            }
        }
        
        [self passByValue];
    }
    
    //下一个；
    if ([sender.currentTitle isEqualToString:@"下一个"])
    {
        if ([array count] > 0)
        {
            currentIndex++;
            if (currentIndex >= [array count])
            {
                currentIndex = 0;
            }
        }
        
        [self passByValue];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clearTextField
{
    self.nameTextField.text = @"";
    self.sexTextField.text = @"";
    self.positionTextField.text = @"";
    self.phoneNumberTextField.text = @"";
    self.companyTextField.text = @"";
    self.letterBoxTextField.text = @"";
    self.weiXinTextField.text = @"";
}

- (void)passByValue
{
    p = array[currentIndex];
    self.nameTextField.text = p.name;
    self.sexTextField.text = p.sex;
    self.companyTextField.text = p.company;
    self.positionTextField.text = p.position;
    self.phoneNumberTextField.text = p.phoneNumber;
    self.weiXinTextField.text = p.weiXin;
    self.letterBoxTextField.text = p.letterBox;
    
}
@end
